﻿using System;
using System.Collections.Generic;
using System.Linq;
using Autodesk.Revit.DB;

namespace ExtendedFilter.ExtendedFilter
{
	public class CategoryItemData : ItemData
	{
		public CategoryItemData(CategoryItemData rhs)
		{
			this.m_Category = rhs.m_Category;
			this.m_Selected = rhs.m_Selected;
			this.m_FamilyItemList = rhs.m_FamilyItemList;
		}

		public CategoryItemData(Category category, List<FamilyItemData> familyItemList)
		{
			this.m_Category = category;
			this.m_Selected = true;
			this.m_FamilyItemList = familyItemList;
		}

		internal static int CategoryItemDataCompare(CategoryItemData data1, CategoryItemData data2)
		{
			string name = data1.m_Category.Name;
			string name2 = data2.m_Category.Name;
			if (name.Length == 0)
			{
				return -1;
			}
			if (name2.Length == 0)
			{
				return 1;
			}
			string gbkX = CategoryItemData.GetGbkX(name.Substring(0, 1));
			string gbkX2 = CategoryItemData.GetGbkX(name2.Substring(0, 1));
			int num = string.Compare(gbkX, gbkX2);
			if (num > 0)
			{
				return 1;
			}
			if (num < 0)
			{
				return -1;
			}
			return 0;
		}

		private static string GetGbkX(string str)
		{
			if (str.CompareTo("吖") < 0)
			{
				return str;
			}
			if (str.CompareTo("八") < 0)
			{
				return "A";
			}
			if (str.CompareTo("嚓") < 0)
			{
				return "B";
			}
			if (str.CompareTo("咑") < 0)
			{
				return "C";
			}
			if (str.CompareTo("妸") < 0)
			{
				return "D";
			}
			if (str.CompareTo("发") < 0)
			{
				return "E";
			}
			if (str.CompareTo("旮") < 0)
			{
				return "F";
			}
			if (str.CompareTo("铪") < 0)
			{
				return "G";
			}
			if (str.CompareTo("讥") < 0)
			{
				return "H";
			}
			if (str.CompareTo("咔") < 0)
			{
				return "J";
			}
			if (str.CompareTo("垃") < 0)
			{
				return "K";
			}
			if (str.CompareTo("呒") < 0)
			{
				return "L";
			}
			if (str.CompareTo("拏") < 0)
			{
				return "M";
			}
			if (str.CompareTo("噢") < 0)
			{
				return "N";
			}
			if (str.CompareTo("妑") < 0)
			{
				return "O";
			}
			if (str.CompareTo("七") < 0)
			{
				return "P";
			}
			if (str.CompareTo("亽") < 0)
			{
				return "Q";
			}
			if (str.CompareTo("仨") < 0)
			{
				return "R";
			}
			if (str.CompareTo("他") < 0)
			{
				return "S";
			}
			if (str.CompareTo("哇") < 0)
			{
				return "T";
			}
			if (str.CompareTo("夕") < 0)
			{
				return "W";
			}
			if (str.CompareTo("丫") < 0)
			{
				return "X";
			}
			if (str.CompareTo("帀") < 0)
			{
				return "Y";
			}
			if (str.CompareTo("咗") < 0)
			{
				return "Z";
			}
			return str;
		}

		public Category TheCategory
		{
			get
			{
				return this.m_Category;
			}
		}

		public bool Selected
		{
			get
			{
				return this.m_Selected;
			}
		}

		public List<FamilyItemData> FamilyItemList
		{
			get
			{
				return this.m_FamilyItemList;
			}
		}

		private int FindFamilyItemIndex(string familyName)
		{
			return this.m_FamilyItemList.FindIndex((FamilyItemData value) => value.TheName.CompareTo(familyName) == 0);
		}

		public void AddElement(Autodesk.Revit.DB.Document doc, Element ele)
		{
			string text = ele.Category.Name;
			if (ele.GetParameters("族").Count != 0)
			{
				text = ele.GetParameters("族").First<Parameter>().AsValueString();
			}
			int num = this.FindFamilyItemIndex(text);
			if (-1 == num)
			{
				List<TypeItemData> typeItemList = new List<TypeItemData>();
				List<PropertyInfo> list = new List<PropertyInfo>();
				foreach (object obj in ele.Parameters)
				{
					Parameter parameter = (Parameter)obj;
					if (!parameter.IsReadOnly)
					{
						string name = parameter.Definition.Name;
						PropertyInfo parInfo = new PropertyInfo(name, false);
						int num2 = list.FindIndex((PropertyInfo value) => value.IsEquals(parInfo));
						if (-1 == num2)
						{
							list.Add(parInfo);
						}
					}
				}
				list.Sort(new PropertyInfoComparer());
				FamilyItemData familyItemData = new FamilyItemData(text, typeItemList, list);
				familyItemData.AddElement(doc, ele);
				this.m_FamilyItemList.Add(familyItemData);
				return;
			}
			this.m_FamilyItemList[num].AddElement(doc, ele);
		}

		public override List<Element> GetSelectElements(FilterType filterType)
		{
			List<Element> list = new List<Element>();
			foreach (FamilyItemData familyItemData in this.m_FamilyItemList)
			{
				list.AddRange(familyItemData.GetSelectElements(filterType));
			}
			return list;
		}

		public override List<Element> GetAllElements()
		{
			List<Element> list = new List<Element>();
			foreach (FamilyItemData familyItemData in this.m_FamilyItemList)
			{
				list.AddRange(familyItemData.GetAllElements());
			}
			return list;
		}

		public override void SetSelected(bool selected)
		{
			this.m_Selected = selected;
			foreach (FamilyItemData familyItemData in this.m_FamilyItemList)
			{
				familyItemData.SetSelected(selected);
			}
		}

		private Category m_Category;

		private bool m_Selected = true;

		private List<FamilyItemData> m_FamilyItemList = new List<FamilyItemData>();
	}
}
